package com.fr.chart.core.glyph;

import com.fr.base.core.DateUtils;
import com.fr.base.core.GraphHelper;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.chart.axis.Axis;
import com.fr.chart.axis.CategoryAxis;
import com.fr.chart.core.ChartUtils;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Stroke;
import java.awt.geom.Line2D;
import java.awt.geom.Line2D.Double;
import java.awt.geom.Point2D;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class DateAxisGlyph extends CategoryAxisGlyph
{
  private static final long serialVersionUID = -317755190103222589L;
  public static final String XML_TAG = "DateAxisGlyph";

  public DateAxisGlyph()
  {
  }

  public DateAxisGlyph(CategoryAxis paramCategoryAxis)
  {
    super(paramCategoryAxis);
  }

  protected Point2D getPoint2DCateAxis(double paramDouble1, double paramDouble2)
  {
    return getPoint2D(ChartUtils.date2Int(DateUtils.object2Date(getCategoryLabelList().get((int)paramDouble1), true)));
  }

  public void calculateAxisLengthUnit()
  {
    this.unitLength = (this.axisLength / (getMaxValue() - getMinValue()));
  }

  public void initMinMaxValue(double paramDouble1, double paramDouble2)
  {
    if (this.categoryAxis.isCustomMinValue())
    {
      setMinValue(this.categoryAxis.getMinValue());
      paramDouble1 = getMinValue();
    }
    else
    {
      setMinValue(paramDouble1);
    }
    if (this.categoryAxis.isCustomMaxValue())
    {
      setMaxValue(this.categoryAxis.getMaxValue());
      paramDouble2 = getMaxValue();
    }
    if (paramDouble2 - paramDouble1 > 0.0D)
    {
      if (this.categoryAxis.isCustomMainDate())
        this.tickLength = ((CategoryAxis)getAxis()).getMainDate();
      else
        this.tickLength = Axis.calculateIncrement(getMinValue(), paramDouble2);
      if (this.categoryAxis.isCustomSecondDate())
        this.smallTickLength = ((CategoryAxis)getAxis()).getSecondDate();
      else
        this.smallTickLength = ((int)this.tickLength / 5);
    }
    if ((paramDouble2 - paramDouble1) % this.tickLength != 0.0D)
      paramDouble2 = paramDouble2 + this.tickLength - (paramDouble2 - paramDouble1) % this.tickLength;
    if (!(this.categoryAxis.isCustomMaxValue()))
      setMaxValue(paramDouble2 + this.tickLength);
    if (getMinValue() >= getMaxValue())
      setMaxValue(getMinValue() + 10.0D);
  }

  protected void drawAxisGrid(Graphics paramGraphics)
  {
    if ((getAxis().getSecondGridStyle() != 0) && ((int)this.smallTickLength > 0))
      drawGridLine(paramGraphics, getAxis().getSecondGridStyle(), getAxis().getSecondGridColor(), (int)this.smallTickLength, this.categoryAxis.getSecondType());
    if ((getAxis().getMainGridStyle() != 0) && ((int)this.tickLength > 0))
      drawGridLine(paramGraphics, getAxis().getMainGridStyle(), getAxis().getMainGridColor(), (int)this.tickLength, this.categoryAxis.getMainType());
  }

  private void drawGridLine(Graphics paramGraphics, int paramInt1, Color paramColor, int paramInt2, int paramInt3)
  {
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    Stroke localStroke = localGraphics2D.getStroke();
    Paint localPaint = localGraphics2D.getPaint();
    localGraphics2D.setStroke(GraphHelper.getStroke(paramInt1));
    localGraphics2D.setPaint(paramColor);
    Date localDate = ChartUtils.int2Date((int)getMinValue());
    while ((ChartUtils.date2Int(localDate) <= getMaxValue()) && (paramInt2 > 0))
    {
      double d = ChartUtils.date2Int(localDate);
      Line2D[] arrayOfLine2D = getGridLine(d);
      for (int i = 0; i < arrayOfLine2D.length; ++i)
        localGraphics2D.draw(arrayOfLine2D[i]);
      nextDate(localDate, paramInt3, paramInt2);
    }
    localGraphics2D.setPaint(localPaint);
    localGraphics2D.setStroke(localStroke);
  }

  protected void drawAxisLine(Graphics paramGraphics)
  {
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    Paint localPaint = localGraphics2D.getPaint();
    Color localColor = getAxis().getLineColor();
    int i = getAxis().getLineStyle();
    if ((localColor != null) && (i != 0) && (getAxis().isAxisShow()))
    {
      localGraphics2D.setPaint(localColor);
      if (getMinValue() == getMaxValue())
        return;
      Line2D.Double localDouble = new Line2D.Double(getPoint2DForSelf(getMinValue()), getPoint2DForSelf(getMaxValue()));
      GraphHelper.draw(localGraphics2D, localDouble, i);
      drawArrow(localGraphics2D, getPoint2DForSelf(getMinValue()), getPoint2DForSelf(getMaxValue()));
    }
    drawTicks(paramGraphics);
    localGraphics2D.setPaint(localPaint);
  }

  public void drawTicks(Graphics paramGraphics)
  {
    BasicStroke localBasicStroke1 = new BasicStroke(this.categoryAxis.getMainStroke(), 1, 0);
    BasicStroke localBasicStroke2 = new BasicStroke(this.categoryAxis.getSecStroke(), 1, 0);
    if ((getAxis().isAxisShow()) && (getMaxValue() > getMinValue()))
    {
      Date localDate1 = ChartUtils.int2Date((int)getMinValue());
      while ((ChartUtils.date2Int(localDate1) <= getMaxValue()) && ((int)this.smallTickLength > 0))
      {
        double d1 = ChartUtils.date2Int(localDate1);
        Line2D localLine2D1 = getTickLine(d1, this.categoryAxis.getSecTickLength(), this.categoryAxis.getSecTickMarkType());
        drawTickLine(paramGraphics, localLine2D1, localBasicStroke2);
        nextDate(localDate1, this.categoryAxis.getSecondType(), (int)this.smallTickLength);
      }
      Date localDate2 = ChartUtils.int2Date((int)getMinValue());
      int i = 0;
      while ((ChartUtils.date2Int(localDate2) <= getMaxValue()) && ((int)this.tickLength > 0))
      {
        Object localObject;
        double d2 = ChartUtils.date2Int(localDate2);
        Line2D localLine2D2 = getTickLine(d2, this.categoryAxis.getTickLength(), this.categoryAxis.getTickMarkType());
        drawTickLine(paramGraphics, localLine2D2, localBasicStroke1);
        String str = "";
        if (this.categoryAxis.getFormat() != null)
        {
          localObject = this.categoryAxis.getFormat();
          str = ((Format)localObject).format(localDate2);
        }
        else
        {
          localObject = new SimpleDateFormat("MM-dd");
          str = ((Format)localObject).format(localDate2);
        }
        if (getAxis().isStagger())
          drawBGLabel(paramGraphics, d2, 0.0D, str, i);
        else
          drawLabel(paramGraphics, d2, 0.0D, str);
        ++i;
        int j = 0;
        if ((getAxis().getLabelNumber() == -1) || (getAxis().getLabelNumber() == 0))
          j = 1;
        else
          j = getAxis().getLabelNumber();
        nextDate(localDate2, this.categoryAxis.getMainType(), (int)this.tickLength * j);
      }
    }
  }

  public static void nextDate(Date paramDate, int paramInt1, int paramInt2)
  {
    if (paramInt1 == 1)
      paramDate.setDate(paramDate.getDate() + paramInt2);
    else if (paramInt1 == 2)
      paramDate.setMonth(paramDate.getMonth() + paramInt2);
    else if (paramInt1 == 3)
      paramDate.setYear(paramDate.getYear() + paramInt2);
  }

  public double getCrossValue()
  {
    return getMinValue();
  }

  public void setAxis(Axis paramAxis)
  {
    if ((paramAxis instanceof CategoryAxis) && (((CategoryAxis)paramAxis).isDateAxis()))
      this.categoryAxis = ((CategoryAxis)paramAxis);
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str1 = paramXMLableReader.getTagName();
      if (str1.equals("DateAxisAttr"))
      {
        String str2;
        if ((str2 = paramXMLableReader.getAttr("minValue")) != null)
          setMinValue(Double.valueOf(str2).doubleValue());
        if ((str2 = paramXMLableReader.getAttr("maxValue")) != null)
          setMaxValue(Double.valueOf(str2).doubleValue());
      }
      else if (str1.equals("CategoryAxis"))
      {
        this.categoryAxis = ((CategoryAxis)paramXMLableReader.readXMLObject(new CategoryAxis()));
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("DateAxisGlyph");
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof DateAxisGlyph))
      return false;
    DateAxisGlyph localDateAxisGlyph = (DateAxisGlyph)paramObject;
    return (super.equals(localDateAxisGlyph));
  }

  public JSONObject toJSONObject()
    throws JSONException
  {
    JSONObject localJSONObject = super.toJSONObject();
    return localJSONObject;
  }

  public String getJSAxisType()
  {
    return "DateAxisGlyph";
  }

  public void axisExtends(int paramInt1, int paramInt2)
  {
    setMaxValue(getMaxValue() + paramInt1);
    setMinValue(getMinValue() - paramInt2);
    calculateAxisLengthUnit();
  }
}